<?php

class PriorityController extends GeanyController {

    
    const _FUNCTION_ID = 'priority';
    
    
    public function __construct(){
        parent::__construct();
        $this->loadDefaultValue();
        $this->loadServices();
    }
    
    
    public function index(){
        
        $data['Model'] = $this->mService->getList();
        
        return View::make('priority/index', $data);
    
    }
    
    public function create(){
        
        
        $input = Input::all();
        $priority = new Priority();
        
           try {
                if($input==null){
                    return $this->createInputView($priority);
                }
                else{
                    $priority = $this->bindData();
                    if($this->validateInput($input)==FALSE) return $this->createInputView($priority);
                    $newPriorityId = $this->mService->insertObject($priority);
                    if($newPriorityId!=null){
                        Return Redirect::to('priority/detail/'.$newPriorityId);
                    }
                    return $this->createInputView($priority);
                }
            } catch (Exception $exc) {
                PageData::addModelError($exc->getMessage());
                return $this->createInputView($this->bindData());
            }     
        
    }
    
      public function edit($id=null){

        
        $input = Input::all();
        $priority = new Priority();
        try {
            if($input==null){
                $priority = $this->mService->getObject($id);
                return $this->createInputView($priority);
            }
            else{
                $priority = $this->bindData();
                if($this->validateInput($input)==FALSE) return $this->createInputView($priority);
                if($this->mService->updateObject($priority,$id)==true){
                    Return Redirect::to('priority/detail/'.$priority->getId());
                }
                return $this->createInputView($priority);
            }
            } catch (Exception $exc) {
                PageData::addModelError($exc->getMessage());
                return $this->createInputView($this->bindData());
            }
        
        }
    
    public function delete($id=null){

        try {
            $this->mService->deleteObject($id);
            
            Return Redirect::to('priority');
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            Return Redirect::to('priority');
        }
    
    }
    
    public function detail($id=null){
        
        $result = $this->mService->getObject($id);
        if($result==null) {
            PageData::addModelError("Priority [ ".$id." ] Not Found ");
            Return Redirect::to('priority')->with('ModelState',PageData::getModelState());
        }
        $data['Model'] = $result;
        $priorityService = new PriorityService();
        $data['Priority'] = $priorityService->getObject($id);
        return View::make('priority/detail', $data);
	}
    
    private function validateInput($data){
        
        if($data==null) return false;
        $rules = array(
            'name' => 'required',
        );

        $messages = array(
            'name' => 'Username salah atau karakter kurang panjang',
        );
        
        
        $this->validator = Validator::make($data, $rules, $messages);
        if($this->validator->fails()) PageData::assignValidatorToModelState ($this->validator);
        return $this->validator->passes();
    }
    
    private function createInputView($priority=null){
        $this->data['Model'] = $priority;
        return View::make('priority/input', $this->data);
    }
    
    private function bindData(){
        $data = Input::all();
        
        $priority = new Priority();
        
        $priority->setName($data["name"]);
        $priority->setLevel($data["level"]);
        $priority->setDescription($data["description"]);

        
        return $priority;
    }

        
    private function loadDefaultValue(){
        $this->setFunctionId(self::_FUNCTION_ID);
        $this->page_data['_MODULE_NAME'] = $this->getFunctionName();
    }
    
    private function loadServices(){
        $this->mService = new PriorityService();
    
    }
    
    
}